package hz;

import java.net.*;
import java.util.concurrent.ConcurrentHashMap;
import java.io.*;

public class GameServer {

	private static final ConcurrentHashMap<Integer, GameThread> threads = new ConcurrentHashMap<>();

	public static void main(String[] args) {

		int port = args.length == 1 ? Integer.parseInt(args[0]) : 60001;

		try (ServerSocket socket = new ServerSocket(port);) {
			while (true) {
				new GameThread(socket.accept()).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void add(int ownerid, GameThread t) {
		System.out.println("Attach thread to " + ownerid);
		threads.put(ownerid, t);
	}

	public static boolean sayTo(int toId, String word) {
		GameThread t = threads.get(toId);
		if (t != null) {
			t.say(word);
			return true;
		}
		return false;
	}
}
